<template>
  <div>
    <!-- 布局容器 -->
    <el-container>
      <!-- 头部区域 -->
      <el-header class="home-head">
        <span>电商后台管理系统</span>
        <el-button type="info" @click="loginout"> 退出 </el-button>
      </el-header>
      <!-- 侧边栏和主体区域 -->
      <el-container class="home-size">
        <!-- 侧边栏区域 -->
        <el-aside :width="isShrink ? '64px' : '200px'" class="home-aside">
          <div class="toggle-button" @click="toggleShrink">|||</div>
          <!-- 侧边栏菜单区域 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            unique-opened
            :collapse="isShrink"
            router
          >
            <!-- 一级菜单 -->
            <el-submenu
              v-for="item in menulist"
              :key="item.id"
              :index="item.id + ''"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item.authName }}</span>
              </template>

              <!-- 二级菜单 -->
              <el-menu-item
                :index="subitem.id + ''"
                v-for="subitem in item.children"
                :key="subitem.id"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subitem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>


<script>
export default {
  data() {
    return {
      menulist: [],
      //   控制展开折叠
      isShrink: false,
    };
  },
  created() {
    this.getMeanList();
  },
  methods: {
    toggleShrink() {
      this.isShrink = !this.isShrink;
    },
    //    获取侧边栏菜单列表
    async getMeanList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    //   退出登录
    loginout() {
      // 1.清除token
      window.sessionStorage.clear();
      // 2.跳转到登录页面
      this.$router.push("/login");
    },
  },
};
</script>

<style scoped>
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.home-head {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.home-aside {
  background-color: #333744;
  height: 100%;
}
.aside-menu {
  background-color: pink;
}
.home-size {
  height: 700px;
}
.el-menu {
  border-right: none;
}
</style>